<template>
    <div>
        <el-main>

            <el-breadcrumb separator="/" style="display: inline-block;margin-left: 10px">
                <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            </el-breadcrumb>

            <div style="padding: 10px 0">
                <el-input style="width: 200px;margin-left: 5px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="username"> </el-input>
                <el-input style="width: 200px;margin-left: 5px" placeholder="请输入电话" suffix-icon="el-icon-message" v-model="phone" > </el-input>
                <el-input style="width: 200px;margin-left: 5px" placeholder="请输入地址" suffix-icon="el-icon-position" v-model="address" > </el-input>
                <el-button style="margin-left: 5px" type="primary" @click="load">搜索</el-button>
                <el-button style="margin-left: 5px" type="warning" @click="reset">重置</el-button>
            </div>
            <div style="margin: 10px 0">
                <el-button type="primary" @click="newadd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
                <el-popconfirm
                        confirm-button-text='确定'
                        cancel-button-text='我在想想'
                        icon="el-icon-info"
                        icon-color="red"
                        title="这是一段内容确定删除吗？"
                        @confirm="alldelete"
                >
                    <el-button type="danger"  slot="reference" style="margin: 0px 15px">批量删除 <i class="el-icon-remove-outline"></i></el-button>
                </el-popconfirm>
                <el-upload action="http://localhost:9090/user/import" :show-file-list="false" :accept="xlsx" :on-success="imp" style="display:inline-block;">
                    <el-button type="primary">导入 <i class="el-icon-bottom"></i></el-button>
                </el-upload>
                <el-button type="primary" @click="exp" style="margin-left: 15px">导出 <i class="el-icon-top"></i></el-button>
            </div>

            <el-table :data="tableData" border stripe  @selection-change="handleSelectionChange">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column width="140" prop="id" label="ID" >
                </el-table-column>
                <el-table-column prop="username" label="姓名" width="140">
                </el-table-column>
                <el-table-column prop="nickname" label="昵称" width="120">
                </el-table-column>
                <el-table-column prop="email" label="邮箱">
                </el-table-column>
                <el-table-column prop="phone" label="电话">
                </el-table-column>
                <el-table-column prop="address" label="地址">
                </el-table-column>
                <el-table-column label="操作"  width="200" align="center">
                    <template slot-scope="scope">
                        <el-button type="success" @click="update(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
                        <el-popconfirm
                                confirm-button-text='确定'
                                cancel-button-text='我在想想'
                                icon="el-icon-info"
                                icon-color="red"
                                title="这是一段内容确定删除吗？"
                                @confirm="remove(scope.row.id)"
                        >
                            <el-button type="danger" slot="reference"  style="margin-left: 15px">删除 <i class="el-icon-remove-outline"></i></el-button>
                        </el-popconfirm>

                    </template>
                </el-table-column>
            </el-table>
            <div style="padding: 10px 0">
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="pageNum"
                        :page-sizes="[2, 5, 10]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>


                <el-dialog :title="title" :visible.sync="dialogFormVisible" width="30%">
                    <el-form :model="form" >
                        <el-form-item label="姓名" >
                            <el-input v-model="form.username" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="昵称" >
                            <el-input v-model="form.nickname" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="邮箱" >
                            <el-input v-model="form.email" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="电话" >
                            <el-input v-model="form.phone" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="地址" >
                            <el-input v-model="form.address" autocomplete="off"></el-input>
                        </el-form-item>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取 消</el-button>
                        <el-button type="primary" @click="Add">确 定</el-button>
                    </div>
                </el-dialog>

            </div>
        </el-main>
    </div>
</template>

<script>
    import request from "@/utils/request";

    export default {
        name: "User",
        data(){
            return {
                tableData: [],
                total:0,
                pageSize:8,
                pageNum:1,
                username:"",
                address:"",
                phone:"",
                dialogFormVisible:false,
                title:'新增',
                form:{},
                multipleSelection: [],
            }
        },
        created() {
            this.load()
        },
        methods:{
            handleSizeChange(pageSize){
                this.pageSize=pageSize
                this.load()
            },
            handleCurrentChange(pageNum){
                this.pageNum=pageNum
                this.load()
            },
            load(){
                request.get("http://localhost:9090/user/page?pageNum="+this.pageNum+"&pageSize="+this.pageSize+"&username="+this.username+"&phone="+this.phone+"&address="+this.address).then(res=>{
                    this.tableData=res.records
                    this.total=res.total
                })

            },
            reset(){
                this.username=""
                this.phone=""
                this.address=""
                this.load()
            },
            newadd(){
                this.title="新增"
                this.dialogFormVisible=true
                this.form={}
            },
            Add(){
                request.post("http://localhost:9090/user",this.form).then(res=>{
                    if(res){
                        this.$message.success("成功")
                        this.load()
                    }else
                        this.$message.error("失败")
                })
                //this.form=""
                this.dialogFormVisible=false
            },
            remove(e){
                request.delete("http://localhost:9090/user/"+e).then(res=>{
                    if(res){
                        this.$message.success("成功")
                        this.load()
                    }else
                        this.$message.error("失败")
                })
            },
            handleSelectionChange(val){
                this.multipleSelection = val;
            },
            alldelete(){
                let ids=this.multipleSelection.map(v=>v.id)
                request.delete("http://localhost:9090/user/all",{data:ids}).then(res=>{
                    if(res){
                        this.$message.success("批量删除成功")
                        this.load()
                    }else
                        this.$message.error("失败")
                })
            },
            update(e){
                this.title='修改'
                this.form=e
                this.dialogFormVisible=true
            },
            exp(){
                window.open("http://localhost:9090/user/export")
            },
            imp(){
                this.$message.success("导入成功")
                this.load()
            }
        }
    }
</script>

<style scoped>

</style>